package com.xiaoyung.easyisapisdk.common.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xiaoyung.easyisapisdk.common.util.CamelFieldNameUtils;
import com.xiaoyung.easyisapisdk.common.util.PageData;

public class PageList<E> extends ArrayList<E> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 页码，从1开始
	 */
	private Integer pageNo;// Num
	/**
	 * 页面大小
	 */
	private Integer pageSize;

	private Integer total;//统一成Integer吧 

	@SuppressWarnings("unchecked")
	public static <T> PageList<T> buildPage(JSONObject rowsData,Class<T> returnItemType,boolean camleNameModel){
		
		JSONArray list = (rowsData.getJSONArray("list"));
		Integer total = rowsData.getInteger("total");
		Integer pageNo = rowsData.getInteger("pageNo");
		Integer pageSize = rowsData.getInteger("pageSize");
		/*this.pageNo = pageNo ;
		this.pageSize = pageSize; 
		this.total = total;*/
		PageList<T> page = new PageList<>();
		if(list!=null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				JSONObject row = list.getJSONObject(i);
				// StringUtils.underlineToCamel(null);
				Object javaObj  ;
				if(returnItemType!=null) {
					if(JSONObject.class.isAssignableFrom(returnItemType)) {
						javaObj = row;
					}else if(PageData.class.isAssignableFrom(returnItemType)) {
						javaObj = new PageData(row);
					}else if(Map.class.isAssignableFrom(returnItemType)) {
						javaObj = new HashMap<>(row);
					}else {
						//java bean
						//翻译 field_xxx 下划线转驼峰 fieldXxx
						if(!camleNameModel) {//当前不是驼峰模式 就要转成驼峰,在构造对象 java对象一定是驼峰模式的
							CamelFieldNameUtils.underlineKeyToCamel(row);
						}
						javaObj = JSON.to(returnItemType,row);
					}
				}else {
					javaObj = row;
				}
				
				page.add((T)javaObj);
			}
		}
		page.setTotal(total);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return page;
	}
	
	public PageList(){
		
	}
	/*public Page(RowsData<E> rowsData){
		super(rowsData.getList());
		this.pageNo = rowsData.getPageNo(); 
		this.pageSize = rowsData.getPageSize(); 
		this.total = rowsData.getTotal() == null ? null : rowsData.getTotal().intValue();
	}*/
	
	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	

	
}
